package cn.lyn4ever.cart.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.lyn4ever.cart.service.CartService;
import cn.lyn4ever.common.MyApiErrors;
import cn.lyn4ever.common.R;
import cn.lyn4ever.entity.Cart;
import cn.lyn4ever.tool.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 微信公众号 “小鱼与Java”
 *
 * @date 2020/4/19
 * @auther Lyn4ever
 */
@RestController
@RequestMapping("cart")
@Api("购物车相关的接口")
public class CartController {

    @Resource
    private CartService cartService;

    @ApiOperation("新增一个商品到购物车")
    @PostMapping("insertOne")
    public R addOne(@RequestBody Cart cart) {
        Assert.NotNull(cart, MyApiErrors.VALIDATE_FAILED);
        QueryWrapper<Cart> wrapper = new QueryWrapper<Cart>();
        wrapper.eq("skuid", cart.getSkuid());
        wrapper.eq("zt", "1");
        Cart one = cartService.getOne(wrapper);
        if (null != one) {
            cart.setId(one.getId());
            cart.setCount(one.getCount() + cart.getCount());
        }
        cart.setCreateTime(new Date());
        cart.setUpdateTime(new Date());
        cart.setZt("1");

        boolean save = cartService.saveOrUpdate(cart);
        return save ? R.ok("添加成功") : R.error("添加失败");
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "userid", value = "用户id", required = true),
            @ApiImplicitParam(name = "storeid", value = "店铺id", required = true),
            @ApiImplicitParam(name = "id", value = "商品id", required = false),
    })
    @ApiOperation("删除购物车中的东西")
    @DeleteMapping("deleteOne/{userid}/{storeid}/{id}")
    public R delOne(@PathVariable("id") Integer id, @PathVariable("storeid") Integer storeid, @PathVariable("userid") Integer userid) {
        QueryWrapper<Cart> wrapper = new QueryWrapper<Cart>();
        wrapper.eq("userid", userid);
        wrapper.eq("storeid", storeid);
        if (id != null && id != 0) {
            wrapper.eq("id", id);
        }
        boolean remove = cartService.remove(wrapper);
        return remove ? R.ok() : R.error();
    }

    @ApiOperation("获取购物车列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userid", value = "用户id", required = true),
            @ApiImplicitParam(name = "storeid", value = "店铺id", required = true),
            @ApiImplicitParam(name = "id", value = "商品id", required = false),
    })
    @GetMapping("getList/{userid}")
    public R getList(@PathVariable("userid") Integer userid) {
        Assert.NotNull(userid,MyApiErrors.VALIDATE_FAILED);
        List<Cart> list = cartService.getList(userid);

        if(list.isEmpty()){
            return R.error("没有数据");
        }
        List list1 = new ArrayList();
        list.stream().forEach(cart -> {
          list1.add(BeanUtil.beanToMap(cart,false,true));
        });
        return R.ok().put("data",list1);
    }

    @ApiOperation("获取购物车数量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userid", value = "用户id", required = true)
    })
    @GetMapping("getCount/{userid}")
    public R getCount(@PathVariable("userid") Integer userid) {
        QueryWrapper<Cart> wrapper = new QueryWrapper<Cart>();
        wrapper.eq("zt", "1");
        wrapper.eq("userid", userid);
        int count = cartService.count(wrapper);
        return R.ok().put("data",count);
    }

}
